package com.anjubao.parking.municipal.roadside.service;

import com.anjubao.parking.municipal.roadside.form.VideoPileForm;
import com.anjubao.parking.municipal.roadside.mapper.DeviceStatusMapper;
import com.anjubao.parking.municipal.roadside.mapper.DeviceVideoPileMapper;
import com.anjubao.parking.municipal.roadside.mapper.DeviceVideoPileSlotMapper;
import com.anjubao.parking.municipal.roadside.model.DeviceStatus;
import com.anjubao.parking.municipal.roadside.model.DeviceVideoPile;
import com.anjubao.parking.municipal.roadside.model.DeviceVideoPileSlot;
import com.anjubao.parking.municipal.roadside.util.FormTransfer;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.ArrayList;

/**
 * 设备同步接口
 * @author : anjubao
 *
 */
@Service
public class DeviceVideoPileService {

    @Autowired
    private DeviceVideoPileMapper deviceVideoPileMapper;
    @Autowired
    private DeviceVideoPileSlotMapper deviceVideoPileSlotMapper;
    @Autowired
    private DeviceStatusMapper deviceStatusMapper;

    /**
     * 根据form表单,构造三个实体
     *
     * @param videoPileForm
     */
    public void syncVideoPile(VideoPileForm videoPileForm) {
        //构造视频桩实体
        DeviceVideoPile deviceVideoPile = FormTransfer.from2DeviceVideoPile(videoPileForm);
        //构造视频桩与车位实体
        List<DeviceVideoPileSlot> deviceVideoPileSlotList = new ArrayList();
        List<Long> parkingLotSlotIds = videoPileForm.getParkingLotSlotId();
        for (Long parkingLotSlotId : parkingLotSlotIds) {
            DeviceVideoPileSlot deviceVideoPileSlot = new DeviceVideoPileSlot();
            deviceVideoPileSlot.setDeviceVideoPileId(videoPileForm.getDeviceId());
            deviceVideoPileSlot.setParkingLotSlotId(parkingLotSlotId);
            deviceVideoPileSlotList.add(deviceVideoPileSlot);
        }
        //构造设备状态实体
        DeviceStatus deviceStatus = FormTransfer.from2DeviceStatus(videoPileForm);
        deviceVideoPileMapper.insertOrUpdate(deviceVideoPile);
        deviceVideoPileSlotMapper.insertOrUpdate(deviceVideoPileSlotList);
        deviceStatusMapper.insertOrUpdate(deviceStatus);

    }

    /**
     * 根据主键删除视频桩、视频桩车位、设备状态表数据
     *
     * @param deviceID         设备Id
     */
    public void deleteVideoPile(Long deviceID) {

        deviceVideoPileMapper.delete(new QueryWrapper<DeviceVideoPile>().eq("id", deviceID));
        deviceVideoPileSlotMapper.delete(new QueryWrapper<DeviceVideoPileSlot>().
                eq("device_video_pile_id", deviceID));
        deviceStatusMapper.delete(new QueryWrapper<DeviceStatus>().eq("device_id", deviceID));
    }
}
